$(() => {
  let baseUrl1 = "http://localhost:3000/api";
  let baseUrl2 = "http://localhost:3000/my";
  let baseUrl3 = "http://localhost:3000/my/artcate";
  let baseUrl4 = "http://localhost:3000/my/article";

  $("#regisBtn").on("click", () => {
    let username = $("#username").val();
    let password = $("#password").val();
    $.ajax({
      type: "post",
      url: baseUrl1 + "/reguser",
      data: {
        username,
        password,
      },
      success: (res) => {
        console.log(res);
        alert(res.msg);
      },
    });
  });

  $("#loginBtn").on("click", () => {
    let username = $("#username2").val();
    let password = $("#password2").val();
    $.ajax({
      type: "post",
      url: baseUrl1 + "/login",
      data: {
        username,
        password,
      },
      success: (res) => {
        console.log(res);
        alert(res.msg);
        if (res.status == 200) {
          localStorage.setItem("token", res.token);
        }
      },
    });
  });

  $("#userInfoBtn").on("click", () => {
    let token = localStorage.getItem("token");
    $.ajax({
      url: baseUrl2 + "/userinfo",
      headers: {
        Authorization: token,
      },
      success: (res) => {
        console.log(res);
        if (res.status == 0) {
          console.log("ok");
          $(".userInfo").eq(0).text(JSON.stringify(res.data));
        }
      },
    });
  });

  $("#updateUserBtn").on("click", () => {
    let token = localStorage.getItem("token");
    let email = $("#email").val();
    let nickname = $("#nickname").val();
    $.ajax({
      type: "post",
      url: baseUrl2 + "/updateUserInfo",
      data: {
        nickname,
        email,
      },
      headers: {
        Authorization: token,
      },
      success: (res) => {
        console.log(res);
        /* if (res.status == 200) {
          localStorage.setItem("token", res.token);
        } */
      },
    });
  });

  $("#updatePwdBtn").on("click", () => {
    let token = localStorage.getItem("token");
    let oldPwd = $("#oldPwd").val();
    let newPwd = $("#newPwd").val();
    $.ajax({
      type: "post",
      url: baseUrl2 + "/updatepwd",
      data: {
        oldPwd,
        newPwd,
      },
      headers: {
        Authorization: token,
      },
      success: (res) => {
        console.log(res);
        /* if (res.status == 200) {
          localStorage.setItem("token", res.token);
        } */
      },
    });
  });

  $("#updateavatarBtn").on("click", () => {
    let token = localStorage.getItem("token");
    let avatar = $("#avatar").val();
    console.log(avatar);
    $.ajax({
      type: "post",
      url: baseUrl2 + "/update/avatar",
      data: {
        avatar,
      },
      headers: {
        Authorization: token,
      },
      success: (res) => {
        console.log(res);
        /* if (res.status == 200) {
          localStorage.setItem("token", res.token);
        } */
      },
    });
  });

  $("#getCatesBtn").on("click", () => {
    let token = localStorage.getItem("token");
    $.ajax({
      url: baseUrl3 + "/cates",
      headers: {
        Authorization: token,
      },
      success: (res) => {
        console.log(res);
        if (res.status == 0) {
          $(".cates").eq(0).text(JSON.stringify(res.data));
        }
      },
    });
  });

  $("#addCateBtn").on("click", () => {
    let token = localStorage.getItem("token");
    let name = $("#addcate_name").val();
    let alias = $("#addcate_alias").val();
    console.log(avatar);
    $.ajax({
      type: "post",
      url: baseUrl3 + "/addcates",
      data: {
        name,
        alias,
      },
      headers: {
        Authorization: token,
      },
      success: (res) => {
        console.log(res);
        /* if (res.status == 200) {
          localStorage.setItem("token", res.token);
        } */
      },
    });
  });

  $("#delCateBtn").on("click", () => {
    let token = localStorage.getItem("token");
    let id = $("#delcate").val();
    console.log(id);
    $.ajax({
      url: baseUrl3 + "/deletecate/" + id,
      headers: {
        Authorization: token,
      },
      success: (res) => {
        console.log(res);
      },
    });
  });

  $("#getCateBtn").on("click", () => {
    let token = localStorage.getItem("token");
    let id = $("#getCate").val();
    $.ajax({
      url: baseUrl3 + "/cates/" + id,
      headers: {
        Authorization: token,
      },
      success: (res) => {
        console.log(res);
        if (res.status == 0) {
          $(".cate").eq(0).text(JSON.stringify(res.data));
        }
      },
    });
  });

  $("#updateCateBtn").on("click", () => {
    let token = localStorage.getItem("token");
    let name = $("#updatecate_name").val();
    let alias = $("#updatecate_alias").val();
    let id = $("#updatecate_id").val();
    $.ajax({
      type: "post",
      url: baseUrl3 + "/updatecate",
      data: {
        name,
        alias,
        id,
      },
      headers: {
        Authorization: token,
      },
      success: (res) => {
        console.log(res);
      },
    });
  });

  $("#publishBtn").on("click", () => {
    let token = localStorage.getItem("token");
    var file = $("#cover_img").get(0).files;
    if (file.length <= 0) {
      return alert("请先上传文件");
    }
    let title = $("#title").val();
    let cate_id = $("#cate_id").val();
    let content = $("#content").val();
    let state = $("#state").val();

    var formData = new FormData();
    formData.append("cover_img", file[0]);
    formData.append("title", title);
    formData.append("cate_id", cate_id);
    formData.append("content", content);
    formData.append("state", state);

    $.ajax({
      type: "post",
      url: baseUrl4 + "/add",
      data: formData,
      processData: false,
      contentType: false,
      headers: {
        Authorization: token,
      },
      success: (res) => {
        console.log(res);
      },
    });
  });

  $("#getArticleBtn").on("click", () => {
    let token = localStorage.getItem("token");
    let state = $("#state1").val();
    let cate_id = $("#cate_id1").val();
    $.ajax({
      url: baseUrl4 + "/list",
      data: {
        state,
        cate_id,
      },
      headers: {
        Authorization: token,
      },
      success: (res) => {
        console.log(res);
      },
    });
  });

  $("#getArticleIDBtn").on("click", () => {
    let token = localStorage.getItem("token");
    let id = $("#id1").val();
    $.ajax({
      url: baseUrl4 + "/article/" + id,
      headers: {
        Authorization: token,
      },
      success: (res) => {
        console.log(res.data[0].cover_img);
        if (res.status == 0) {
          $("#img").prop(
            "src",
            `http://localhost:3000${res.data[0].cover_img}`
          );
        }
      },
    });
  });

  $("#delArticleIDBtn").on("click", () => {
    let token = localStorage.getItem("token");
    let id = $("#id2").val();
    $.ajax({
      url: baseUrl4 + "/delete/" + id,
      headers: {
        Authorization: token,
      },
      success: (res) => {
        console.log(res);
      },
    });
  });

  $("#editBtn").on("click", () => {
    let token = localStorage.getItem("token");
    var file = $("#editcover_img").get(0).files;
    if (file.length <= 0) {
      return alert("请先上传文件");
    }
    let title = $("#edittitle").val();
    let cate_id = $("#editcate_id").val();
    let content = $("#editcontent").val();
    let state = $("#editstate").val();
    let id = $("#editid").val();

    var formData = new FormData();
    formData.append("cover_img", file[0]);
    formData.append("title", title);
    formData.append("cate_id", cate_id);
    formData.append("content", content);
    formData.append("state", state);
    formData.append("id", id);

    $.ajax({
      type: "post",
      url: baseUrl4 + "/edit",
      data: formData,
      processData: false,
      contentType: false,
      headers: {
        Authorization: token,
      },
      success: (res) => {
        console.log(res);
      },
    });
  });

  let dealData = () => {
    let sql1 = "select * from  ev_articles";

    let res1 = await sqlPromise(sql1);

    console.log(res1);

    // res.send(res1)

    let sql2 = "select * from  ev_article_cate";

    let res2 = await sqlPromise(sql2);

    console.log(res2);

    let data = res1.reduce((prevl, curvl, index) => {
      let { id, title, pub_date, state } = curvl;

      let [{ name: cate_name }] = res2.filter((item) => {
        if (item.id == curvl.cate_id) return item;
      });

      // let newarr=[{cate_name:'最新'} ,{cate_name:'科技'} ]

      // prevl[index] = { id, title, pub_date, state, cate_name };
      prevl.push({ id, title, pub_date, state, cate_name });
      return prevl;
    }, []);
  };
});
